package Java8实战._3_Stream_用流收集数据._4_分区;

import Java8实战._3_Stream_用流收集数据.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class _1_分区的优势 {

    public static void main(String[] args) {
        List<Dish> menu = Arrays.asList(
                new Dish("pork",false,800, Dish.Type.MEAT),
                new Dish("beef",false,700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH)
        );

        Map<Boolean, Map<Dish.Type, List<Dish>>> collect = menu.stream()
                .collect(
                        Collectors.partitioningBy(Dish::isVegetarian,
                                Collectors.groupingBy(Dish::getType)
                        )
                );

        for (Map.Entry<Boolean,Map<Dish.Type,List<Dish>>> map : collect.entrySet()){

            System.out.println("map的大小：" + collect.size());

            boolean isTrue = map.getKey();
            System.out.println(isTrue);
            if(isTrue){
                Map<Dish.Type,List<Dish>> map1 = map.getValue();
                for (Map.Entry<Dish.Type,List<Dish>> map2 : map1.entrySet()){
                    System.out.println("\t" + map2.getKey());
                    List<Dish> list = map2.getValue();
                    for (Dish dish : list){
                        System.out.println("\t\t" + dish.getName() + ", " + dish.getCalories());
                    }
                }
            }
            else{
                Map<Dish.Type,List<Dish>> map1 = map.getValue();
                for (Map.Entry<Dish.Type,List<Dish>> map2 : map1.entrySet()){
                    System.out.println("\t" + map2.getKey());
                    List<Dish> list = map2.getValue();
                    for (Dish dish : list){
                        System.out.println("\t\t" + dish.getName() + ", " + dish.getCalories());
                    }
                }
            }

        }
    }
}















